package nobodyCodesThat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class CharacterTools {
	public static final int CHAR_TYPE = 0;

	public static final int EXP_POINTS = 1;

	public static String getCharacterAttrib(String pathWithProfile, int attrib) {
		pathWithProfile = pathWithProfile + "\\Player.chr";
		InputStreamReader fileInput = null;
		File f = new File(pathWithProfile);
		byte[] buffer = new byte[(int) f.length()];
		try {
			fileInput = new InputStreamReader(new FileInputStream(new File(
					pathWithProfile)));
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}
		BufferedReader bufferedReader = new BufferedReader(fileInput);
		int i = 0;
		try {
			while (bufferedReader.read() != -1) {
				buffer[i] = (byte) bufferedReader.read();
				i++;
			}
			System.out.println(HexDump.toHexAsciiString(buffer, 32));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	private static String getCharType(StringBuffer playerConfigFile) {
		if (playerConfigFile.substring(48, 55).equalsIgnoreCase("warrior")) {
			return "Warrior";
		} else if (playerConfigFile.substring(48, 57).equalsIgnoreCase(
				"sorceress")) {
			return "Sorceress";
		} else {
			return null;
		}
	}

	private static String getExperiencePoints(StringBuffer playerConfigFile) {
		String searchPattern = "experiencePoints";
		int amountOfBits = 8;
		int test = playerConfigFile.indexOf(searchPattern);
		String newTest = playerConfigFile.substring(test
				+ searchPattern.length(), test + searchPattern.length()
				+ amountOfBits);
		StringBuffer testBufferArray = new StringBuffer();
		char[] testArray = newTest.toCharArray();
		for (int i = 0; i < testArray.length; i++) {
			// testBufferArray.append(Character.getNumericValue(testArray[i]));
		}
		System.out.println("Array: " + testBufferArray);
		// System.out.println("Hier: "
		// + playerConfigFile.substring(test, test + 100));
		System.out.println("Hier: " + newTest);
		// System.out.println("Hier: " + Integer.parseInt(newTest, 16));
		return null;

	}

	/**
	 * Konvertiert ein Byte in einen Hex-String.
	 */
	private static String toHexString(byte b) {
		int value = (b & 0x7F) + (b < 0 ? 128 : 0);
		String ret = (value < 16 ? "0" : "");
		ret += Integer.toHexString(value).toUpperCase();
		return ret;
	}

	public static void main(String[] args) {
		String test = getCharacterAttrib(System.getProperty("user.home")
				+ Constants.TQSAVES + "\\_Sirion", EXP_POINTS);
		System.out.println("This is: " + test);
	}

}
